package limeirafol.gui.Cadastro;

import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.SwingConstants;
import limeirafol.Global;
import org.openide.util.Exceptions;
import limeirafol.bean.BeanCadastro;
import limeirafol.gui.ParametroGUI;
import limeirafol.remote.cadastro.BanhoFiltroConsultaVO;
import limeirafol.remote.cadastro.BanhoVO;
import lbframework.bean.internalFrame.LBInternalFrame;
import lbframework.bean.mdiFrame.LBMdiFrame;
import lbframework.bean.table.LBColumnTable;
import lbframework.classe.Util;
import lbframework.classe.LBException;
import lbframework.remote.ItemComboVO;
import limeirafol.remote.Formulario;
import limeirafol.remote.TipoOperacao;

public class BanhoConsultaGUI extends LBInternalFrame {
//Teste

    private Vector<BanhoVO> vBanho = null;
    public BeanCadastro beanCadastro = new BeanCadastro();

    public BanhoConsultaGUI(LBMdiFrame i_mdiFrame) throws Exception {
        super(i_mdiFrame);

        initComponents();

        centralizarForm();

        this.setMaximum(true);


        cboSituacao.setTabela("situacaocadastro");
        cboSituacao.carregar();
        cboSituacao.addItem(new ItemComboVO(-1, "TODOS"));

    }

    @Override
    public void editar() throws Exception {
        carregarBanho();
    }

    @Override
    public void parametrizar() throws Exception {
        Global.verificarPermissao(Formulario.BANHO, TipoOperacao.PARAMETRIZAR);

        ParametroGUI form = new ParametroGUI(mdiFrame);

        form.carregarParametro(3, 4, 5);
        form.setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        vRPanel1 = new lbframework.bean.panel.LBPanel();
        vRLabel1 = new lbframework.bean.label.LBLabel();
        txtDescricao = new lbframework.bean.textField.LBTextField();
        vRLabel2 = new lbframework.bean.label.LBLabel();
        cboSituacao = new lbframework.bean.comboBox.LBComboBox();
        vRLabel3 = new lbframework.bean.label.LBLabel();
        txtCodigo = new lbframework.bean.textField.LBTextField();
        txtSigla = new lbframework.bean.textField.LBTextField();
        vRLabel4 = new lbframework.bean.label.LBLabel();
        chkBanhoAcessorio = new lbframework.bean.checkBox.LBCheckBox();
        chkUtilizaMilesimos = new lbframework.bean.checkBox.LBCheckBox();
        tblConsulta = new lbframework.bean.tableEx.LBTableEx();
        vRPanel2 = new lbframework.bean.panel.LBPanel();
        vRButton1 = new lbframework.bean.button.LBButton();
        vRButton2 = new lbframework.bean.button.LBButton();
        vRToolBarPadrao1 = new lbframework.bean.toolBarPadrao.LBToolBarPadrao(this);

        setTitle("Banho Consulta");

        vRLabel1.setText("Descrição");

        vRLabel2.setText("Ativo");

        vRLabel3.setText("Código");

        vRLabel4.setText("Sigla");

        chkBanhoAcessorio.setText("Banho Acessório");

        chkUtilizaMilesimos.setText("Utiliza Milésimos");

        javax.swing.GroupLayout vRPanel1Layout = new javax.swing.GroupLayout(vRPanel1);
        vRPanel1.setLayout(vRPanel1Layout);
        vRPanel1Layout.setHorizontalGroup(
            vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vRLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vRLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vRLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSigla, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vRLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addComponent(cboSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(chkBanhoAcessorio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(chkUtilizaMilesimos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        vRPanel1Layout.setVerticalGroup(
            vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addComponent(vRLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cboSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(chkBanhoAcessorio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(chkUtilizaMilesimos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addComponent(vRLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtSigla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addComponent(vRLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addComponent(vRLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tblConsulta.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblConsultaMouseClicked(evt);
            }
        });
        tblConsulta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tblConsultaKeyPressed(evt);
            }
        });

        vRButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/sair.png"))); // NOI18N
        vRButton1.setText("Sair");
        vRButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vRButton1ActionPerformed(evt);
            }
        });

        vRButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/consultar.png"))); // NOI18N
        vRButton2.setText("Consultar");
        vRButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vRButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout vRPanel2Layout = new javax.swing.GroupLayout(vRPanel2);
        vRPanel2.setLayout(vRPanel2Layout);
        vRPanel2Layout.setHorizontalGroup(
            vRPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, vRPanel2Layout.createSequentialGroup()
                .addContainerGap(576, Short.MAX_VALUE)
                .addComponent(vRButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(vRButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        vRPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {vRButton1, vRButton2});

        vRPanel2Layout.setVerticalGroup(
            vRPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(vRButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(vRButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        vRToolBarPadrao1.setRollover(true);
        vRToolBarPadrao1.setVisibleConsultar(true);
        vRToolBarPadrao1.setVisibleEditar(true);
        vRToolBarPadrao1.setVisibleExcluir(true);
        vRToolBarPadrao1.setVisibleImprimir(true);
        vRToolBarPadrao1.setVisibleIncluir(true);
        vRToolBarPadrao1.setVisibleParametrizar(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(vRPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(tblConsulta, javax.swing.GroupLayout.DEFAULT_SIZE, 806, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(vRPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(vRToolBarPadrao1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(vRToolBarPadrao1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(vRPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(tblConsulta, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(vRPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void vRButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vRButton1ActionPerformed

        try {
            sair();
        } catch (Exception ex) {
            Exceptions.printStackTrace(ex);
        }
    }//GEN-LAST:event_vRButton1ActionPerformed

    private void vRButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vRButton2ActionPerformed
        try {
            consultar();
        } catch (Exception ex) {
            Exceptions.printStackTrace(ex);
        }


    }//GEN-LAST:event_vRButton2ActionPerformed

    private void tblConsultaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblConsultaMouseClicked

        try {
            if (evt.getClickCount() == 2) {
                carregarBanho();
            }

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }

    }//GEN-LAST:event_tblConsultaMouseClicked

    private void tblConsultaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblConsultaKeyPressed

        try {
            if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
                carregarBanho();
            }

            if (evt.getKeyCode() == KeyEvent.VK_DELETE) {
                excluir();
            }

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }


    }//GEN-LAST:event_tblConsultaKeyPressed

    @Override
    public void excluir() throws Exception {
        Global.verificarPermissao(Formulario.BANHO, TipoOperacao.EXCLUIR);

        if (tblConsulta.getLinhaSelecionada() == -1) {
            throw new LBException(Util.MSG_NENHUM_ITEM_SELECIONADO);
        }

        Util.exibirMensagemConfirmar(Util.MSG_CONFIRMA_EXCLUSAO, getTitle());

        BanhoVO oBanho = vBanho.get(tblConsulta.getLinhaSelecionada());

        beanCadastro.excluirBanho(oBanho.id);

        vBanho.remove(oBanho);

        exibirConsulta();
    }

    private void carregarBanho() throws Exception {
        Global.verificarPermissao(Formulario.BANHO, TipoOperacao.ALTERAR);
        if (tblConsulta.getLinhaSelecionada() == -1) {
            throw new LBException(Util.MSG_NENHUM_ITEM_SELECIONADO);
        }
        BanhoVO oBanho = vBanho.get(tblConsulta.getLinhaSelecionada());

        if (consultaCampo == null) {
            BanhoCadastroGUI form = new BanhoCadastroGUI(mdiFrame);

            form.carregarBanho(oBanho.id);
            form.setVisible(true);

        } else {
            this.setVisible(false);

            consultaCampo.setParentSelected(true);
            consultaCampo.setId(oBanho.id);
            consultaCampo.consultar(true);
            consultaCampo.transferFocus();
        }
    }

    @Override
    public void consultar() throws Exception {
        Global.verificarPermissao(Formulario.BANHO, TipoOperacao.CONSULTAR);

        BanhoFiltroConsultaVO oFiltro = new BanhoFiltroConsultaVO();
        oFiltro.id = txtCodigo.getInt();
        oFiltro.descricao = txtDescricao.getText();
        oFiltro.sigla = txtSigla.getText();
        oFiltro.id_situacaoCadastro = cboSituacao.getId();
        oFiltro.banhoacessorio = chkBanhoAcessorio.isSelected();
        oFiltro.utilizaml = chkUtilizaMilesimos.isSelected();

        vBanho = beanCadastro.consultarBanho(oFiltro);

        exibirConsulta();
    }

    @Override
    public void incluir() throws Exception {
        Global.verificarPermissao(Formulario.BANHO, TipoOperacao.INCLUIR);

        BanhoCadastroGUI form = new BanhoCadastroGUI(mdiFrame);

        form.incluir();
        form.setVisible(true);

    }

    @Override
    public void imprimir() throws Exception {
        Global.verificarPermissao(Formulario.BANHO, TipoOperacao.IMPRIMIR);
        Global.imprimir("banho.jasper", vBanho, null);
    }

    private void exibirConsulta() throws Exception {
        LBColumnTable[] colunas = new LBColumnTable[]{
            new LBColumnTable("Código", 60, true, SwingConstants.LEFT, false, null),
            new LBColumnTable("Descricao", 200, true, SwingConstants.LEFT, false, null),
            new LBColumnTable("Sigla", 100, true, SwingConstants.LEFT, false, null),
            new LBColumnTable("Banho Acessório", 100, true, SwingConstants.LEFT, false, null),
            new LBColumnTable("Utiliza Milésimos", 100, true, SwingConstants.LEFT, false, null),
            new LBColumnTable("Preço Por Kilo", 80, true, SwingConstants.LEFT, false, null),
            new LBColumnTable("Situação", 100, true, SwingConstants.LEFT, false, null)
        };
        Object[][] dados = new Object[vBanho.size()][colunas.length];
        int i = 0;
        for (BanhoVO oBanho : vBanho) {
            dados[i][0] = Util.formatNumber(oBanho.id, 6);
            dados[i][1] = oBanho.descricao;
            dados[i][2] = oBanho.sigla;
            dados[i][3] = oBanho.banhoacessorio;
            dados[i][4] = oBanho.utilizamilesimos;
            dados[i][5] = Util.formatDecimal2(oBanho.precoporkilo);
            dados[i][6] = oBanho.situacao;

            i++;
        }

        tblConsulta.setModel(dados, colunas);

        tblConsulta.requestFocus();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private lbframework.bean.comboBox.LBComboBox cboSituacao;
    private lbframework.bean.checkBox.LBCheckBox chkBanhoAcessorio;
    private lbframework.bean.checkBox.LBCheckBox chkUtilizaMilesimos;
    private lbframework.bean.tableEx.LBTableEx tblConsulta;
    private lbframework.bean.textField.LBTextField txtCodigo;
    private lbframework.bean.textField.LBTextField txtDescricao;
    private lbframework.bean.textField.LBTextField txtSigla;
    private lbframework.bean.button.LBButton vRButton1;
    private lbframework.bean.button.LBButton vRButton2;
    private lbframework.bean.label.LBLabel vRLabel1;
    private lbframework.bean.label.LBLabel vRLabel2;
    private lbframework.bean.label.LBLabel vRLabel3;
    private lbframework.bean.label.LBLabel vRLabel4;
    private lbframework.bean.panel.LBPanel vRPanel1;
    private lbframework.bean.panel.LBPanel vRPanel2;
    private lbframework.bean.toolBarPadrao.LBToolBarPadrao vRToolBarPadrao1;
    // End of variables declaration//GEN-END:variables
}
